package com.ocom.webapi.controller;

import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ImageTypeChecker;
import com.ocom.common.utils.ResultUtil;
import com.ocom.webapi.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;

@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
@RestController
public class MinioController {

    @Autowired
    MinioService minioService;

    /**
     * 文件上传（UpgradePage/importUpgrade）
     * */
    @PostMapping(HumanServiceUrl.URL_FileLoad)
    public Result FileLoad(@RequestPart("file") MultipartFile file){

        String fileName = file.getName();
        String ext = fileName.substring(fileName.lastIndexOf(".")+1);
        boolean ifExcel = ext.equals("xls") || ext.equals("xlsx");

        String type = ImageTypeChecker.getImageTypeFromBytes(fileName.getBytes(StandardCharsets.UTF_8));

        if(type == null && !ifExcel){
            return ResultUtil.error(ResultEnum.Err_4203);
        }
        return minioService.FileLoad(file);
    }
}
